home *** CD-ROM | disk | FTP | other *** search
- /*
- File: TDrawContext.cp
-
- Contains: Implementation of a layer class on top of QuickDraw
-
- Written by: Arno Gourdol
-
- Copyright: © 1994-1995 by Apple Computer, Inc., all rights reserved.
-
- */
-
- #include "TDrawContext.h"
- #include "TBitmap.h"
-
- #include "macros.h"
-
-
-
- // --------------------------------------------------------------------
- // MovePenTo
- // --------------------------------------------------------------------
-
- void TDrawContext::MovePenTo(CPoint pt)
- {
- assert(fLockCount > 0);
-
- ::MoveTo(pt.X(), pt.Y());
- }
-
-
-
- // --------------------------------------------------------------------
- // MovePenTo
- // --------------------------------------------------------------------
-
- void TDrawContext::MovePenTo(GraphicalUnit x, GraphicalUnit y)
- {
- assert(fLockCount > 0);
-
- ::MoveTo(x, y);
- }
-
-
-
- // --------------------------------------------------------------------
- // MovePenBy
- // --------------------------------------------------------------------
-
- void TDrawContext::MovePenBy(GraphicalUnit dx, GraphicalUnit dy)
- {
- assert(fLockCount > 0);
-
- ::Move(dx, dy);
- }
-
-
-
- // --------------------------------------------------------------------
- // PenLocation
- // --------------------------------------------------------------------
-
- CPoint TDrawContext::PenLocation(void) const
- {
- assert(fLockCount > 0);
-
- PenState penState;
- ::GetPenState(&penState);
- return CPoint(penState.pnLoc);
- }
-
-
-
- // --------------------------------------------------------------------
- // StrokeLine
- // --------------------------------------------------------------------
-
- void TDrawContext::StrokeLine(CPoint toPt)
- {
- assert(fLockCount > 0);
-
- ::LineTo(toPt.X(), toPt.Y());
- }
-
-
-
- // --------------------------------------------------------------------
- // StrokeLine
- // --------------------------------------------------------------------
-
- void TDrawContext::StrokeLine(CPoint pt0, CPoint pt1)
- {
- assert(fLockCount > 0);
-
- ::MoveTo(pt0.X(), pt0.Y());
- ::LineTo(pt1.X(), pt1.Y());
- }
-
-
-
- // --------------------------------------------------------------------
- // StrokeRect
- // --------------------------------------------------------------------
-
- void TDrawContext::StrokeRect(const CRect& rect)
- {
- assert(fLockCount > 0);
-
- ::FrameRect(rect);
- }
-
-
-
- // --------------------------------------------------------------------
- // FillRect
- // --------------------------------------------------------------------
-
- void TDrawContext::FillRect(const CRect& rect)
- {
- assert(fLockCount > 0);
-
- ::PaintRect(rect);
- }
-
-
-
- // --------------------------------------------------------------------
- // InvertRect
- // --------------------------------------------------------------------
-
- void TDrawContext::InvertRect(const CRect& rect)
- {
- assert(fLockCount > 0);
-
- ::InvertRect(rect);
- }
-
-
- /*
-
-
-
- // --------------------------------------------------------------------
- // MovePenTo
- // --------------------------------------------------------------------
-
- void TDrawContext::FrameInsetBox(const CRect& rect) const
- {
- CRect tempRect(rect);
-
- InsetRect(tempRect, 1, 1);
- this->Frame(tempRect);
- InsetRect(tempRect, -1, -1);
-
- this->LineFromTo(tempRect.Left(), tempRect.Bottom() - 2, tempRect.Left(), tempRect.Top(), kInsetBoxShadow);
- this->LineTo(tempRect.Right() - 2, tempRect.Top(), kInsetBoxShadow);
-
-
- this->LineFromTo(tempRect.Right() - 1, tempRect.Top() + 1, tempRect.Right() - 1, tempRect.Bottom() - 1, kInsetBoxHilite);
- this->LineTo(tempRect.Left(), tempRect.Bottom() - 1, kInsetBoxHilite);
-
- ::ForeColor(blackColor);
- }
- */
-
- /*
-
-
-
- // --------------------------------------------------------------------
- // MovePenTo
- // --------------------------------------------------------------------
-
- void TDrawContext::FrameBevel(const CRect& rect,
- GraphicalUnit leftOffset,
- GraphicalUnit rightOffset)
- {
- CRect tempRect(rect);
-
- leftOffset = pin(rect.Left() + leftOffset, rect.Left(), rect.Right()) - rect.Left();
- rightOffset = rect.Right() - pin(rect.Right() - rightOffset, rect.Left(), rect.Right());
-
- if (leftOffset == rightOffset)
- {
- SetHighColor(CColor(kBevelShadowColor));
- StrokeLine(tempRect.LeftTop(), CPoint(tempRect.Right() - 2, tempRect.Top()));
- StrokeLine(CPoint(tempRect.Right() - 2, tempRect.Bottom() - 2));
- StrokeLine(CPoint(tempRect.Left(), tempRect.Bottom() - 2));
- StrokeLine(CPoint(tempRect.Left(), tempRect.Top()));
-
- // Left side
- SetHighColor(CColor(kBevelHiliteColor));
- MovePenTo(tempRect.Left() + 1, tempRect.Bottom() - 3);
- StrokeLine(CPoint(tempRect.Left() + 1, tempRect.Top() + 1));
-
- // Top side
- StrokeLine(CPoint(tempRect.Right() - 3, tempRect.Top() + 1));
-
- // Right side
- MovePenTo(CPoint(tempRect.Right() - 1, tempRect.Top()));
- StrokeLine(CPoint(tempRect.Right() - 1, tempRect.Bottom() - 1));
-
- // Bottom side
- StrokeLine(CPoint(tempRect.Left(), tempRect.Bottom() - 1));
- }
- else
- {
- // Top left side
- this->LineFromTo(tempRect.Left(), tempRect.Top(), tempRect.Left() + leftOffset, tempRect.Top(), kBevelShadowIndex);
- this->LineFromTo(tempRect.Left() + 1, tempRect.Top() + 1, tempRect.Left() + leftOffset, tempRect.Top() + 1, kWhiteIndex);
-
- // Top right side
- this->LineFromTo(tempRect.Right() - rightOffset, tempRect.Top(), tempRect.Right() - 1, tempRect.Top(), kBevelShadowIndex);
- this->LineFromTo(tempRect.Right() - rightOffset, tempRect.Top() + 1, tempRect.Right() - 2, tempRect.Top() + 1, kWhiteIndex);
-
- // Left side
- this->LineFromTo(tempRect.Left(), tempRect.Top(), tempRect.Left(), tempRect.Bottom() - 1, kBevelShadowIndex);
- this->LineFromTo(tempRect.Left() + 1, tempRect.Top() + 1, tempRect.Left() + 1, tempRect.Bottom() - 2, kWhiteIndex);
-
- // Right side
- this->LineFromTo(tempRect.Right() - 1, tempRect.Top(), tempRect.Right() - 1, tempRect.Bottom() - 1, kBevelShadowIndex);
- this->LineFromTo(tempRect.Right(), tempRect.Top(), tempRect.Right(), tempRect.Bottom(), kWhiteIndex);
-
- // Bottom side
- this->LineFromTo(tempRect.Left(), tempRect.Bottom() - 1, tempRect.Right() - 1, tempRect.Bottom() - 1, kBevelShadowIndex);
- this->LineFromTo(tempRect.Left(), tempRect.Bottom(), tempRect.Right(), tempRect.Bottom(), kWhiteIndex);
- }
-
- ::ForeColor(blackColor);
- }
-
- */
-
-
-
- // --------------------------------------------------------------------
- // StrokeRegion
- // --------------------------------------------------------------------
-
- void TDrawContext::StrokeRegion(const RgnHandle rgn)
- {
- assert(fLockCount > 0);
-
- ::FrameRgn(rgn);
- }
-
-
-
- // --------------------------------------------------------------------
- // FillRegion
- // --------------------------------------------------------------------
-
- void TDrawContext::FillRegion(const RgnHandle rgn)
- {
- assert(fLockCount > 0);
-
- ::PaintRgn(rgn);
- }
-
-
-
- // --------------------------------------------------------------------
- // DrawBitmap
- // --------------------------------------------------------------------
-
- void TDrawContext::DrawBitmap(const TBitmap* bitmap) const
- {
- assert(bitmap != NULL);
- GWorldPtr grafWorld = bitmap->GetGWorldPtr();
-
- if (grafWorld != NULL)
- {
- ::CopyBits(&((GrafPtr)grafWorld)->portBits,
- &fPort->portBits, bitmap->Bounds(),
- bitmap->Bounds(), srcCopy, NULL);
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // DrawBitmap
- // --------------------------------------------------------------------
-
- void TDrawContext::DrawBitmap(const TBitmap* bitmap, CPoint where) const
- {
- #pragma unused(bitmap, where)
-
-
- }
-
-
-
- // --------------------------------------------------------------------
- // DrawBitmap
- // --------------------------------------------------------------------
-
- void TDrawContext::DrawBitmap(const TBitmap* bitmap, CRect dstRect,
- RgnHandle mask) const
- {
- assert(bitmap != NULL);
- GWorldPtr grafWorld = bitmap->GetGWorldPtr();
-
- if (grafWorld != NULL)
- {
- ::CopyBits(&((GrafPtr)grafWorld)->portBits,
- &fPort->portBits, bitmap->Bounds(),
- dstRect, srcCopy, mask);
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // MovePenTo
- // --------------------------------------------------------------------
-
- void TDrawContext::DrawBitmap(const TBitmap* bitmap, CRect dstRect) const
- {
- assert(bitmap != NULL);
- GWorldPtr grafWorld = bitmap->GetGWorldPtr();
-
- if (grafWorld != NULL)
- {
- ::CopyBits(&((GrafPtr)grafWorld)->portBits,
- &fPort->portBits, bitmap->Bounds(),
- dstRect, srcCopy, NULL);
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // MovePenTo
- // --------------------------------------------------------------------
-
- void TDrawContext::DrawBitmap(const TBitmap* bitmap, CRect srcRect, CRect dstRect) const
- {
- assert(bitmap != NULL);
- GWorldPtr grafWorld = bitmap->GetGWorldPtr();
-
- if (grafWorld != NULL)
- {
- ::CopyBits(&((GrafPtr)grafWorld)->portBits,
- &fPort->portBits, srcRect,
- dstRect, srcCopy, NULL);
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // MovePenTo
- // --------------------------------------------------------------------
-
- void TDrawContext::StrokeFocusRing(const CRect& rect)
- {
- assert(fLockCount > 0);
-
- CTempRgn haloRgn;
-
- // Temp rect and its pointer for Quickdraw API calls
- CRect tempRect(rect);
-
- tempRect.InsetBy(-4);
-
- // Build the region of the halo
- ::OpenRgn();
- StrokeRect(tempRect);
- tempRect.InsetBy(2);
- StrokeRect(tempRect);
- ::CloseRgn(haloRgn);
-
- FillRegion(haloRgn);
- }
-
-
-
- // --------------------------------------------------------------------
- // MovePenTo
- // --------------------------------------------------------------------
-
- void TDrawContext::StrokeFocusRing(const RgnHandle rgn)
- {
- assert(fLockCount > 0);
-
- CTempRgn haloRgn = rgn;
- CTempRgn tempRgn = rgn;
-
- ::InsetRgn(haloRgn, -4, -4);
- ::InsetRgn(tempRgn, -2, -2);
- ::DiffRgn(haloRgn, tempRgn, haloRgn);
-
- FillRegion(haloRgn);
- }
-
-
-
- // --------------------------------------------------------------------
- // MovePenTo
- // --------------------------------------------------------------------
-
- void TDrawContext::SetHighColor(CColor color)
- {
- assert(fLockCount > 0);
-
- if (!color.IsBlack() && !color.IsWhite())
- {
- if (!IsColorPort())
- {
- ::ForeColor(whiteColor);
- }
- else
- {
- if ((fDepth >= 8) || (fDepth == 4 && !fIsColor))
- {
- ::RGBForeColor(color);
- }
- else
- {
- ::ForeColor(whiteColor); // not good enough for anything but white
- }
- }
- }
- else
- {
- ::RGBForeColor(color);
- }
- }
-
-
-
- // --------------------------------------------------------------------
- // Lock
- // --------------------------------------------------------------------
- // Call to start drawing in the drawing context.
- // If returns true, you can go ahead.
- // Call Unlock() when done drawing.
-
- Boolean TDrawContext::Lock(void)
- {
- Boolean result;
-
- if (fLockCount == 0)
- {
- ::GetPort(&fSavePort);
- if (fSavePort != NULL)
- {
- ::GetPenState(&fSavePenState);
- ::GetForeColor(&fSaveForeColor);
- ::GetBackColor(&fSaveBackColor);
- }
- assert(fPort != NULL);
- ::SetPort(fPort);
- ::PenNormal();
-
- result = true;
- }
- else
- {
- result = true;
- }
-
- fLockCount++;
-
- return result;
- }
-
-
-
- // --------------------------------------------------------------------
- // Unlock
- // --------------------------------------------------------------------
- // Call when done drawing in the drawing context.
- // Lock() must have been called before and have returned true.
-
- void TDrawContext::Unlock(void)
- {
- assert(fLockCount > 0); // Make sure we were locked
-
- fLockCount--;
-
- if (fLockCount == 0)
- {
- SetPort(fSavePort);
- if (fSavePort != NULL)
- {
- SetPenState(&fSavePenState);
- RGBForeColor(&fSaveForeColor);
- RGBBackColor(&fSaveBackColor);
- fSavePort = NULL;
- }
- }
- }
-
-